package cn.px.rest.controller.purchase;

import cn.px.common.bean.AbstractPageResultBean;
import cn.px.common.util.BeanConverterUtil;
import cn.px.common.util.R;
import cn.px.common.validation.group.AddGroup;
import cn.px.common.validation.group.UpdateGroup;
import cn.px.dao.dto.purchase.PurchaseListDTO;
import cn.px.dao.entity.purchase.PurchaseList;
import cn.px.dao.util.ExcelUtil;
import cn.px.dao.vo.export.purchase.ExportPurchaseListVO;
import cn.px.dao.vo.query.purchase.PurchaseListVO;
import cn.px.rest.log.annotation.SysLog;
import cn.px.service.purchase.PurchaseListService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Description 采购单明细 控制器
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2020,北京乾元坤和科技有限公司 All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("/purchase/purchase-order-lists")
public class PurchaseOrderListController {

    private final PurchaseListService purchaseListService;

    /**
     * 通过id查询单条记录
     *
     * @param id 主键
     * @return R
     */
    @GetMapping("/{id}")
    public R<PurchaseList> getById(@PathVariable("id") String id) {
        return new R<>(purchaseListService.getModelById(id));
    }

    /**
     * 新增记录
     *
     * @param purchaseListDTO 采购单明细DTO
     * @return R
     */
    @SysLog("新增采购单明细")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('purchase-purchase-order-lists-add')")
    public R save(@Validated({AddGroup.class}) @RequestBody PurchaseListDTO purchaseListDTO) {
        return new R<>(purchaseListService.saveModelByDTO(purchaseListDTO));
    }

    /**
     * 修改记录
     *
     * @param purchaseListDTO 采购单明细DTO
     * @return R
     */
    @SysLog("修改采购单明细")
    @PutMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('purchase-purchase-order-lists-edit')")
    public R update(@PathVariable("id") String id, @Validated({UpdateGroup.class}) @RequestBody PurchaseListDTO purchaseListDTO) {
        purchaseListService.modModelByDTO(purchaseListDTO);
        return new R<>();
    }

    /**
     * 通过id删除一条记录
     *
     * @param id 主键
     * @return R
     */
    @SysLog("删除采购单明细")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('purchase-purchase-order-lists-del')")
    public R removeById(@PathVariable String id) {
        purchaseListService.deleteByPrimaryKey(id);
        return new R<>();
    }

    /**
     * 批量记录
     *
     * @param ids 主键
     * @return R
     */
    @SysLog("批量删除采购单明细")
    @DeleteMapping("")
    @PreAuthorize("@pms.hasPermission('purchase-purchase-order-lists-del')")
    public R removeIds(@RequestBody List<String> ids) {
        purchaseListService.batchDelete(ids);
        return new R<>();
    }

    /**
     * 获取分页
     *
     * @param pageNum         当前页码
     * @param pageSize        每页条数
     * @param purchaseListDTO 采购单明细DTO
     * @return R
     */
    @GetMapping("/page")
    public R<AbstractPageResultBean<PurchaseListVO>> query(PurchaseListDTO purchaseListDTO, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "20") int pageSize) {
        return new R<>(purchaseListService.selectPageList(purchaseListDTO, pageNum, pageSize));
    }

    /**
     * 获取列表
     *
     * @param purchaseListDTO 采购单明细DTO
     * @return R
     */
    @GetMapping("")
    public R query(PurchaseListDTO purchaseListDTO) {
        return new R<>(purchaseListService.getModelListByDTO(purchaseListDTO));
    }

    /**
     * 采购单明细导出
     *
     * @param purchaseListDTO 采购单明细DTO
     * @return R
     */
    @GetMapping("/export")
    @SysLog("采购单明细导出")
//    @PreAuthorize("@pms.hasPermission('purchase-purchase-order-lists-export')")
    public void export(HttpServletResponse response, PurchaseListDTO purchaseListDTO) {
        List<PurchaseList> list = purchaseListService.getModelListByDTO(purchaseListDTO);
        ExcelUtil.exportResponseDict(response, ExportPurchaseListVO.class, BeanConverterUtil.copyListProperties(list, ExportPurchaseListVO.class), "采购单明细");
    }

    /**
     * 更新选择采购单明细
     *
     * @param purchaseListDTO 采购单明细DTO
     * @return R
     */
    @SysLog("更新选择采购单明细")
    @PutMapping("/choose")
    @PreAuthorize("@pms.hasPermission('purchase-purchase-order-lists-edit')")
    public R chooseList(@RequestBody PurchaseListDTO purchaseListDTO) {
        purchaseListService.batchUpdateChoose(purchaseListDTO);
        return new R<>();
    }

    /**
     * 随机清单导入
     *
     * @return R
     */
    @PostMapping("/importForEnquiry")
    @SysLog("采购明细询价导入")
    public R importForEnquiry(@RequestParam("file") MultipartFile file, String purchaseOrderId, Integer moneyType, BigDecimal exchangeRate) throws Exception {
        purchaseListService.importListForEnquiry(file, purchaseOrderId, moneyType, exchangeRate);
        return R.success();
    }

    /**
     * 采购明细导出
     *
     * @param purchaseListDTO 采购明细DTO
     * @return R
     */
    @GetMapping("/exportForEnquiry")
    @SysLog("采购明细询价导出")
    @PreAuthorize("@pms.hasPermission('purchase-purchase-order-lists-export')")
    public void exportForEnquiry(HttpServletResponse response, PurchaseListDTO purchaseListDTO) {
        List<PurchaseList> list = purchaseListService.getModelListByDTO(purchaseListDTO);
        ExcelUtil.exportResponseDict(response, ExportPurchaseListVO.class, BeanConverterUtil.copyListProperties(list, ExportPurchaseListVO.class), "采购明细");
    }


}
